<template>
  <a-modal
    :title="`产品${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 5 } }" :wrapperCol="{ sm: { span: 19 } }" :selfUpdate="true">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>

        <a-form-item label="短信类型">
          <a-select v-decorator="['isInland', { rules: [{ required: true, message: '短信类型不能为空' }] }]">
            <a-select-option v-for="it in isInlandList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="名称">
          <a-input
            v-decorator="['name', { rules: [{ required: true, message: '名称不能为空' }] }]"
            placeholder="请输入名称"
          />
        </a-form-item>
        <a-form-item label="平台">
          <AWM-select
            v-decorator="['platformId', { rules: [{ required: true, message: '平台不能为空' }] }]"
            placeholder="请输入平台"
            funStr="platformByNoAuth"
          ></AWM-select>
        </a-form-item>
        <a-form-item label="类型">
          <a-select
            v-decorator="['type', { rules: [{ required: true, message: '类型不能为空' }] }]"
            placeholder="请选择类型"
          >
            <a-select-option v-for="it in templateTypeList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="平台成本单价">
          <a-input v-decorator="['platformPrice', { rules: [{ required: true, message: '平台成本单价不能为空' }] }]" placeholder="请输入平台成本单价" />
        </a-form-item>
        <a-form-item label="用户初始单价">
          <a-input v-decorator="['userPrice', { rules: [{ required: true, message: '用户初始单价不能为空' }] }]" placeholder="请输入用户初始单价" />
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea v-decorator="['remark']" placeholder="请输入备注" />
        </a-form-item>
        <!-- <a-form-item label="角色">
          <a-select v-decorator="['roleId']" placeholder="请选择用户角色">
            <a-select-option v-for="it in roleList" :key="it.id" :value="Number(it.id)">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item> -->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import cloneDeep from 'lodash.clonedeep'
import { isInlandList, templateTypeList } from '@/utils/other'
import AWMSelect from '@/components/AWMSelect'

// 表单字段
const fields = ['id', 'isInland', 'name', 'platformPrice','platformId','userPrice', 'remark', 'type']

export default {
  components: {
    AWMSelect,
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      inviteUserList: [],
      timeout: null,
      fetching: false,
      projectType: [],
      templateTypeList,
      isInlandList,
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', (n) => {
      if (this.model) {
        this.form.setFieldsValue(pick(this.model, fields))
      }
    })
  },
  methods: {
  },
  computed: {
  },
  watch: {},
}
</script>
